#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ctype.h>  
#include <unistd.h>
 
#include "indexer.h"


static void _mkdir(const char *path)
{
        char tmp_path[256];
        char *p;
        size_t len;

        strncpy(tmp_path, path, sizeof(tmp_path));
        len = strlen(tmp_path);

        if(tmp_path[len - 1] == '/')
                tmp_path[len - 1] = '\0';
      	for(p = tmp_path; *p; p++)
               	if(*p == '/') 
		{
                      	*p = '\0';
                        if(access(tmp_path, F_OK))
                                mkdir(tmp_path, S_IRWXU);
                        *p = '/';
                }
        if(access(tmp_path, F_OK))         
                mkdir(tmp_path, S_IRWXU);
}
/////////////////////////////////////////////////////////////////////////////////////////
void normalize_date(char *date)
{

	int i = 0;
	char *month_array[] = {"jan", "feb", "mar", "apr", "may" , "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
	char *month_array_list[] = {"Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "aout", "septembre", "octobre", "novembre", "decembre", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};	
 
	 
	
	for( i = 0; i< 45; i++) 
		if ( strncmp(date, month_array_list[i], 3) == 0 )
			 strcpy(date, month_array[i%12]);

}
/////////////////////////////////////////////////////////////////////////////////////////
char *index_folder_creator(Matcher *current, char *logline, int *ovector, int is_match, char *norm_log_root_path)
{					
 
	Index *index = 	current->Indexs->first;
	printf (" le path est ");
 
 	char *index_value = malloc(sizeof(*index_value) * 1024);
	char *path = malloc(sizeof(*path) * 1024);
	strcpy(path , "");
	strcat(path , norm_log_root_path);
	strcat(path , "/");
	 
 	while (index != NULL)
	{ 	
		pcre_copy_named_substring(current->regex,logline, ovector, is_match, index->name, index_value, 1024);
		//strcat(path , "");
 
		if( strcmp (index->name, "month") == 0) //printf(" ********************** ");//
		{		
			normalize_date(index_value);
			strcat(path , index_value);
		} 
		else 		
		strcat(path , index_value);
		
		strcat(path , "/");
 
		index = index->next;
	}
	printf(" %s créer \n", path);
	_mkdir( path);
	free(index_value);
	return path;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void filed_file_creator(Matcher *current, char *rawlogline, int *ovector, int is_match, char *index_path, char *id, char *timestamp, char *user_name, char *norm_log_file_path, char *pipe_log_path)
{					
	int pipe;

 
	Field *field = 	current->Fields->first;
				 
 	char *field_value = malloc(sizeof(*field_value) * 1024);
	char *field_path = malloc(sizeof(*field_path) * 1024);
 	char *log_line = malloc(sizeof(*log_line) * 1024);
 
	strcpy(field_path , "");
	strcat(field_path , index_path);
	strcat(field_path , norm_log_file_path);

	strcpy(log_line , "");
	strcat(log_line , id);
	strcat(log_line , "|");

	while (field != NULL)
	{ 	
		pcre_copy_named_substring(current->regex,rawlogline, ovector, is_match, field->name, field_value, 1024);
		strcat(log_line , field_value);strcat(log_line , "|");
		field = field->next;
	}
	strcat(log_line , index_path);strcat(log_line , "|");strcat(log_line , timestamp);strcat(log_line , "\0");
	 
	printf("Le field %s créer \n", field_path);

	FILE *fichier = fopen( field_path, "a");
	fseek(fichier, 0L, SEEK_END);     
 
	if (fichier != NULL   )
    	{
        	fputs( log_line , fichier);
        	fclose(fichier);
    	}

	if((pipe = open(pipe_log_path, O_WRONLY)) == -1) 
	{
		fprintf(stderr, "Can't open the named pipe. \n");
		exit(EXIT_FAILURE);
	}

	write(pipe, log_line, strlen(log_line));
 
	
	strcpy(field_path, "");
	strcpy(log_line, "");


	free(log_line);
	free(field_path);
	free(field_value);	
}
